﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BaoJian.Control
{
    public partial class NewsList : System.Web.UI.UserControl
    {
        public string columnID;
        public string CurrentPos;
        public string Region;
        public int SubColumnCount = 8;
        private bool HasChildern = false;


        protected void Page_Load(object sender, EventArgs e)
        {


            columnID = HttpContext.Current.Request.QueryString["p"];
            if (columnID == null)
            {
                HttpContext.Current.Response.Redirect("default.aspx");
            }
            if (!Page.IsPostBack)
            {
                GetCurrentPos();
                ViewState["CurrentPos"] = CurrentPos;
                lblCurrentPage.Text = "1";

                if (HasChildern)
                {
                    DetailDiv.Visible = false;
                    ListColDiv.Visible = true;
                    BindColListData();
                }
                else
                {
                    DetailDiv.Visible = true;
                    ListColDiv.Visible = false;
                    BindData();
                }
            }
            else
            {
                CurrentPos = ViewState["CurrentPos"] as string;
            }
        }         

        private void GetCurrentPos()
        {
            //TODO:这里需要进一步的优化       
            //取得当前栏目的所有父栏目,生成当前位置
            string sqlSysHiber = "oid='{0}' AND (Parentid <> '19800628-0000-0000-0000-000000000001') AND (Parentid <> 'eb8b49b9-e760-497b-83cf-d9fd1d9f0833') AND (Parentid <> 'FD870D78-0DB5-48D5-AF31-4D6ACC3FA90D')";
            sqlSysHiber = string.Format(sqlSysHiber, columnID);
            DataSet hiberds = Tools.Common.GetSysobj_HiberFromCache();
            DataSet objds = Tools.Common.GetSysobjFromCache();
            DataRow[] dr = hiberds.Tables[0].Select(sqlSysHiber, "Plevcod asc");
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < dr.Length; i++)
            {
                DataRow[] d = objds.Tables[0].Select("oid='" + dr[i]["Parentid"] + "'");
                sb.Append("<a href=\"col.aspx?p=" + d[0]["OID"] as string + "\">" + d[0]["oname"] as string + "</a> > >");
            }

            //判断是否有子栏目
            sqlSysHiber = "Parentid='{0}'";
            sqlSysHiber = string.Format(sqlSysHiber, columnID);
            dr = hiberds.Tables[0].Select(sqlSysHiber);
            if (dr.Length > 0)
            {
                HasChildern = true;
            }

            DataRow[] t = objds.Tables[0].Select("oid='" + columnID + "'");
            sb.Append(" <a href=\"col.aspx?p=" + t[0]["OID"] as string + "\">" + t[0]["oname"] as string + "</a> ");

            if (Region == "beijing" || Region == null || Region == "")
            {
                sb.Insert(0,"<a href=\"default.aspx\">首页</a> > >");

            }
            else
            {
                sb.Insert(0,"<a href=\"/" + Region + "/default.aspx\">首页</a> > > ");
            }
            CurrentPos = sb.ToString();


        }
        void BindColListData()
        {
            string sql = "parentid='" + columnID + "' AND Plevcod =1";
            DataSet hiberds = Tools.Common.GetSysobj_HiberFromCache();
            DataSet objds = Tools.Common.GetSysobjFromCache();
            DataRow[] dr = hiberds.Tables[0].Select(sql);

            sql = " select top {0} oid,title,CTime FROM doc where  pid='{1}' ORDER BY sid desc ";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            for (int i = 0; i < dr.Length; i++)
            {
                //生成查询 doc 的sql 条件
                sb.Append(string.Format(sql, SubColumnCount, dr[i]["oid"]));
            }
            // content.Append(" </ul>  </div>");
            DataSet ds = Maticsoft.DBUtility.DbHelperSQL.Query(sb.ToString());

            System.Text.StringBuilder nav = new System.Text.StringBuilder();
            System.Text.StringBuilder content = new System.Text.StringBuilder();


            nav.Append(" <div class=\"middlenav\"> <h4><span class=\"ico3\">快速导航</span></h4><ul>");
            content.Append("  <div class=\"newslist\">");
            for (int i = 0; i < dr.Length; i++)
            {
                //生成导航
                DataRow[] t = objds.Tables[0].Select("oid='" + dr[i]["oid"] + "'");
                nav.Append("<li><a href=\"col.aspx?p=" + t[0]["OID"] + "\">" + t[0]["OName"] + "</a></li>");

                //生成文章列表
                content.Append("<div class=\"lm_name\"><span class=\"tt\">");
                content.Append("<a href=\"col.aspx?p=" + t[0]["OID"] + "\">" + t[0]["OName"] + "</a></span><span class=\"more\"><a href=\"col.aspx?p=" + t[0]["OID"] + "\">更多>></a></span></div><ul>");

                DataTable dt = ds.Tables[i];
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    content.Append("<li><a href=\"c.aspx?id=" + dt.Rows[j]["OID"] + "\">" + dt.Rows[j]["title"] + "</a></li><li class=\"date\">" + ((DateTime)dt.Rows[j]["CTime"]).ToString("yyyy-MM-dd") + "</li>");
                }
                content.Append("</ul><div class=\"line\"></div>");
            }

            nav.Append(" </ul></div>");
            content.Append("</div>");

            ListContent.Text = nav.ToString() + content.ToString();

        }
        void BindData()
        {
            LYQ.BLL.Doc docBll = new LYQ.BLL.Doc();
            string conditon = "pid='" + columnID + "' and Region='" + Region + "' ";
            string order = " order by SID desc ";
            DataSet ds = docBll.GetList(conditon + order);
            DataTable tempTable = ds.Tables[0];

            int pageSize = 20;//每页显示条数
            int reNodeTotalCount = tempTable.Rows.Count; //Tools.Common.GetTotalNum("doc","'''"+ conditon+"'''"); //记录总条数
            int totalPage = (reNodeTotalCount % pageSize == 0 ? reNodeTotalCount / pageSize : reNodeTotalCount / pageSize + 1);//总页数.

            //当前页
            int curpage = Convert.ToInt32(lblCurrentPage.Text);
            if (curpage > 1 && curpage > totalPage)
            {
                curpage = totalPage;
                lblCurrentPage.Text = totalPage.ToString();
            }

            if (tempTable.Rows.Count > 0)
            {
                changePage.Visible = true;
                ///下面是分页显示的程序： 
                PagedDataSource ps = new PagedDataSource();
                ps.AllowPaging = true;
                ps.PageSize = pageSize;
                ps.CurrentPageIndex = curpage - 1;
                ps.DataSource = tempTable.DefaultView;

                ViewState["SumCount"] = totalPage;  //记录总页码,共最后一页翻页时使用

                if (reNodeTotalCount > 0)
                {

                    first_btn.Enabled = true; //首页
                    up_btn.Enabled = true;  //上一页
                    next_btn.Enabled = true; //下一页
                    end_btn.Enabled = true; //末页

                    if (curpage == 1)  //如果是第一页。首页和上一页将不可用.
                    {
                        first_btn.Enabled = false;
                        up_btn.Enabled = false;
                    }

                    if (curpage == ps.PageCount) //如果是最后一页,下一页和末页不可用
                    {
                        next_btn.Enabled = false;
                        end_btn.Enabled = false;
                    }

                    //设置页面的跳转选项数据
                    ddlGo.Visible = true;
                    ddlGo.Items.Clear();
                    for (int i = 1; i <= totalPage; i++)
                    {
                        ListItem item = new ListItem();
                        item.Text = i.ToString();
                        item.Value = i.ToString();
                        ddlGo.Items.Insert(i - 1, item);
                    }
                    ddlGo.SelectedValue = lblCurrentPage.Text;

                }
                Repeater1.DataSource = ps;
                Repeater1.DataBind();
            }

        }

        protected void first_btn_Click(object sender, EventArgs e)
        {
            lblCurrentPage.Text = "1";
            BindData();
        }
        protected void up_btn_Click(object sender, EventArgs e)
        {
            lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text) - 1);
            BindData();
        }
        protected void next_btn_Click(object sender, EventArgs e)
        {
            lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text) + 1);
            BindData();
        }
        protected void end_btn_Click(object sender, EventArgs e)
        {
            lblCurrentPage.Text = ViewState["SumCount"].ToString();
            BindData();
        }
        protected void ddlGo_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblCurrentPage.Text = ddlGo.SelectedValue;
            BindData();
        }
    }
}